<?php
namespace Seller\Controller;
use Think\Controller;

class ApiController extends Controller {
    public function _initialize() {

    }
    //验证key值
    public function checkKey($param,$key){
        unset($param['key']);

        $param = json_encode($param); //将数组转化为json
        //$md5data = md5data($param);
        //echo $md5data;die;
        $result = md5msg($param,$key);
        if($result == 0){
            $this->respons(10000);
        }
    }
    //返回json数据
    public function respons($code, $data = ""){
        $msg= get_error_msg($code);
        $this -> responFunc(array("status"=>$code,"msg"=>$msg,"data"=>$data));
    }

    protected function responFunc(array $respon){
        $this->ajaxReturn($respon);
    }

    /* 空操作，用于输出404页面 */
    public function _empty(){
        $this->redirect('Index/index');
    }

    /* 统一POST数据接收方法，方便以后安全过滤扩展 **/
    protected function post( $valuenames, $important = true ){
        // header:application/json
        if( ! empty($GLOBALS['HTTP_RAW_POST_DATA']) )  $postdata = json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
        // header:application/x-www-form-urlencoded
        elseif( ! empty($_POST) )                      $postdata = (object)($_POST);
        // else returns 100001
        else                                           $this -> respons(10001);

        if( is_string($valuenames) ) {
            $value = isset($postdata->$valuenames) ? trim($postdata->$valuenames) : '';
            return $value;
            /*
            $value = isset($postdata->$valuenames) ? trim($postdata->$valuenames) : '';

            if( '' !== $value )  return $value;
            return  $important ? $this -> respons(10001) : '';*/
        }elseif( is_array($valuenames) ){
            $result = array();
            foreach( $valuenames as $vn ){
                $value = isset($postdata->$vn) ? trim($postdata->$vn) : '';
                if( isset($postdata->$vn) ){
                    $result[$vn] = $value;
                }elseif( substr($vn, -5, 5) === ':null' ){
                    continue;
                }else{
                    if($important){
                        $this -> respons(10001);
                    }else{
                        continue;
                    }
                }
            }
            return $result;
        }
    }

    /**
     * 验证用户提交信息，对比key值
     */
    public function check_post_key($request){
        foreach($request as $k => $v){
            $param[ $v ]=$this->post($v);
        }

        $key = $param['key'];//获取key值
        $this->checkKey($param,$key);//验证key值

        return $param;
    }

    /**
     * 获取post数据
     */
    public function getPost($request = array())
    {
        if (empty($request))
        {
            $this->respons(10001);
        }
        foreach($request as $k => $v)
        {
            $param[ $v ]=$this->post($v);
        }
        return $param;
    }




}
